package com.frxuexi.docment

import android.util.Log
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.frxuexi.docment.entity.DocEntity
import java.text.SimpleDateFormat

@Composable
fun DocPage(data: List<DocEntity>, onClick: () -> Unit, onRemoveClick: (DocEntity) -> Unit) {

    Column {
        Row {
            TextButton(onClick = { onClick() }) {
                Icon(imageVector = Icons.Default.ArrowBack, contentDescription = "")
            }
            Text(
                text = "文件",
                modifier = Modifier.fillMaxWidth(),
                textAlign = TextAlign.Center
            )
        }
        LazyColumn(verticalArrangement = Arrangement.spacedBy(10.dp), content = {
            items(data) {
                DocItem(entity = it) {
                    onRemoveClick(it)
                }
            }
        })
    }
}

@Composable
fun DocItem(entity: DocEntity, onClick: () -> Unit) {
    var isShow by remember {
        mutableStateOf(false)
    }

    Row(modifier = Modifier
        .padding(horizontal = 10.dp)
        .clickable {
            isShow=true

                   }, verticalAlignment = Alignment.CenterVertically
    ) {
        AsyncImage(
            modifier = Modifier
                .size(80.dp)
                .clip(RoundedCornerShape(20.dp)),
            model = entity.filePath, contentDescription = ""
        )


        Spacer(modifier = Modifier.width(10.dp))
        Column {
            //文件名处理
            val name = entity.fileName
            val myname = name.split(".").get(0)

            //时间处理
            val time = entity.fileTime
            var sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            val mytime = sdf.format(time)

            Text(modifier = Modifier.height(40.dp), text = myname, textAlign = TextAlign.Center)
            Text(modifier = Modifier.height(40.dp), text = mytime, textAlign = TextAlign.Center)
        }
    }

    if(isShow){
        AlertDialog(title={ Text(text = "删除")},
            text = { Text(text = "确认删除这条数据吗？")},
            onDismissRequest = {
                isShow=false
            },
            dismissButton = {
                TextButton(
                    onClick = {
                        isShow=false
                    }
                ) {
                    Text("取消")
                }
            },
            confirmButton = {
                TextButton(
                    onClick = {
                        onClick()//删除
                        isShow=false
                    }
                ) {
                    Text("确认")
                }
            })
    }

}